using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class Member_PromotionManage_CouponItemManage : Member, IRequiresSessionState
{
	public string m_flag = "0";
	private string linkhead = "";
	private string beforeurl = "CouponItemManage.aspx?1=1";
	private string tabMenu = "";
	protected Literal ltrTitle;
	protected GridView gvList;
	protected Label PagePanel;
	protected Button ButDel;
	public string TabMenu
	{
		get
		{
			if (base.Request.QueryString["TabMenu"] != null)
			{
				this.tabMenu = base.Request.QueryString["TabMenu"].Trim();
			}
			return this.tabMenu;
		}
		set
		{
			this.tabMenu = value;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			this.BindList();
		}
	}
	protected void BindList()
	{
		string text = this.QuestString();
		string queryByOrder = this.GetQueryByOrder();
		int num = 0;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		int pageSize = this.gvList.PageSize;
		ClientByPage clientByPage = new ClientByPage();
		string text2 = base.SalerGetCouponInfoIDStr(base.user.UserID.ToString());
		DataTable dataTable = null;
		if (!string.IsNullOrEmpty(text2))
		{
			string text3 = " from HL_CouponInfo as info,HL_CouponItem as item where info.CouponInfoID=item.CouponInfoID and item.CouponInfoID in(" + text2 + ") ";
			int listCount = clientByPage.GetListCount("select count(*)" + text3 + text);
			int startIndex = (num - 1) * pageSize + 1;
			int endIndex = num * pageSize;
			string str = "ROW_NUMBER() OVER (" + queryByOrder + ") as row,CouponName,CouponParvalue,NeedsMoney,BeginDate,EndDate,item.* ";
			string strSQL = "select " + str + text3 + text;
			dataTable = clientByPage.GetListPage(strSQL, "row", startIndex, endIndex);
			this.gvList.DataSource = dataTable;
			this.gvList.DataBind();
			if (dataTable.Rows.Count > 0)
			{
				CurrPageLinks currPageLinks = new CurrPageLinks();
				string url = this.beforeurl + this.linkhead + "&pageindex";
				this.PagePanel.Text = currPageLinks.CurrPageLink(num, pageSize, listCount, "条记录&nbsp;&nbsp;每页" + pageSize + "条数据&nbsp;&nbsp;", url);
			}
			else
			{
				this.PagePanel.Text = "";
			}
			for (int i = 0; i < this.gvList.Rows.Count; i++)
			{
				Literal literal = (Literal)this.gvList.Rows[i].FindControl("ltrUserName");
				LinkButton linkButton = (LinkButton)this.gvList.Rows[i].FindControl("btnDelete");
				if (literal != null)
				{
					if (!string.IsNullOrEmpty(literal.Text))
					{
						linkButton.Visible = false;
						this.gvList.Rows[i].Cells[0].Enabled = false;
					}
				}
				Literal literal2 = (Literal)this.gvList.Rows[i].FindControl("ltrUserOrder");
				if (literal2 != null)
				{
					literal2.Text = this.getOrderNumber(literal2.Text);
				}
			}
		}
		else
		{
			this.gvList.DataSource = dataTable;
			this.gvList.DataBind();
		}
	}
	public string getUseStatus(object value)
	{
		string result = "";
		if (value != null)
		{
			if (value.ToString() == "1" || value.ToString().ToLower() == "true")
			{
				result = "<span style='color:Green' >√</span>";
			}
			else
			{
				result = "<span style='color:Red' >×</span>";
			}
		}
		return result;
	}
	public string getOrderNumber(string orderid)
	{
		string result = "";
		if (!string.IsNullOrEmpty(orderid))
		{
			string sql = "select OrderFormNum from HLShop_Order where OrderFormID=" + orderid;
			DataTable dataTable = this.conn.GetDataTable(sql);
			if (dataTable.Rows.Count > 0)
			{
				result = dataTable.Rows[0]["OrderFormNum"].ToString();
			}
		}
		return result;
	}
	protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
		}
	}
	protected string QuestString()
	{
		string text = "";
		string text2 = this.TabMenu;
		if (text2 != null)
		{
			if (!(text2 == "tabAll"))
			{
				if (!(text2 == "tabNoUse"))
				{
					if (!(text2 == "tabUse"))
					{
						if (!(text2 == "tabNoOverdue"))
						{
							if (text2 == "tabOverdue")
							{
								this.m_flag = "4";
								text += " and info.EndDate<GETDATE()";
							}
						}
						else
						{
							this.m_flag = "3";
							text += " and info.EndDate>=GETDATE()";
						}
					}
					else
					{
						this.m_flag = "2";
						text += " and item.UseStatus=1";
					}
				}
				else
				{
					this.m_flag = "1";
					text += " and item.UseStatus=0";
				}
			}
			else
			{
				this.m_flag = "0";
			}
		}
		this.linkhead = this.linkhead + "&TabMenu=" + this.TabMenu;
		return text;
	}
	private string GetQueryByOrder()
	{
		string result = "order by item.CouponItemID desc";
		string text = this.TabMenu;
		if (text != null)
		{
			if (!(text == "tabNoOverdue"))
			{
				if (text == "tabOverdue")
				{
					result = "order by info.EndDate desc";
				}
			}
			else
			{
				result = "order by info.EndDate desc";
			}
		}
		return result;
	}
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string commandName = e.CommandName;
		if (commandName == "MoreDel")
		{
			string text = Text.CheckNumList(Fetch.GetOrPost("chk_select"));
			if (text != "")
			{
				bool flag = false;
				string[] array = text.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					flag = this.delInfo(array[i]);
				}
				if (flag)
				{
					this.WriteErrMsg("", "批量信息删除成功！", this.beforeurl + this.linkhead + "&pageindex");
				}
			}
			else
			{
				this.WriteErrMsg("", "请选您要删除的信息", "");
			}
		}
		else
		{
			if (commandName == "DelInfo")
			{
				string id = e.CommandArgument.ToString();
				bool flag2 = this.delInfo(id);
				if (flag2)
				{
					this.WriteErrMsg("", "删除成功！", this.beforeurl + this.linkhead + "&pageindex");
				}
			}
		}
		this.BindList();
	}
	private bool delInfo(string id)
	{
		bool result = false;
		HoWave.Data.Model.CouponItem model = BLLInstance.BllCouponItem.GetModel(Convert.ToInt32(id));
		if (model != null)
		{
			bool flag = BLLInstance.BllCouponItem.Delete(Convert.ToInt32(id));
			if (flag)
			{
				string sql = "update HL_CouponInfo set CouponCount=CouponCount-1 where CouponInfoID=" + model.CouponInfoID;
				this.conn.Execute(sql);
			}
		}
		return result;
	}
}
